package wh.coach.pivot3.table;

import lombok.NoArgsConstructor;
import wh.coach.domain.CalType;

import java.util.*;

/**
 * @author xhw
 * @date 2024/1/20
 */

@NoArgsConstructor
public class Groups {
    //Integer -> StudentScore
    private final Map<GroupKey, List<StudentScore>> groups = new TreeMap<>();


    public void addStudentScore(final StudentScore studentScore) {
        //行列分组
        GroupKey key = new GroupKey(
                List.of(studentScore.getSchool(), studentScore.getGrade(), studentScore.getClassName()),
                List.of(studentScore.getBranch(), studentScore.getSubject()));

        //添加分组后的分数
        groups.computeIfAbsent(key, k -> new ArrayList<>()).add(studentScore);
    }

    public Number calculate(CalType calType,GroupKey groupKey) {
        List<StudentScore> studentScores = groups.get(groupKey);
        List<Number> scores = studentScores.stream()
                    .map(StudentScore::getScore)
                    .map(Number.class::cast)
                    .toList();
            Aggregation aggregation = new Aggregation(calType, scores);
        return aggregation.calculate();
    }

    public Set<GroupKey> getKetSet() {
        return groups.keySet();
    }
}


